package com.gin.kotlin.vegetables_kot.app

import android.app.Application
import android.os.Handler
import android.os.Message
import android.util.Log
import android.widget.ImageView
import androidx.room.Room
import com.bumptech.glide.Glide
import com.gin.kotlin.vegetables_kot.base.HandlerHolder
import com.gin.kotlin.vegetables_kot.base.MyHandler
import com.gin.kotlin.vegetables_kot.database.VegDatabase
import com.gin.kotlin.vegetables_kot.interceptor.LoggingInterceptor
import com.gin.kotlin.vegetables_kot.network.api.VegRecordApi
import com.gin.retrofit.factory.QueryParameterConverterFactory
import com.gin.retrofit.utils.JacksonUtils
import com.gin.retrofit.utils.OkhttpUtils
import com.wgw.photo.preview.PhotoPreview
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory

/**
 * APP对象
 * @property TAG TAG
 * @property handler [MyHandler]
 * @property versionName APP当前版本
 * @property resourceApi 静态资源API
 * @property database 数据库连接
 * @property account 当前账号
 * @property ngaApi 当前账号的api
 * @property emoteGroup 官方表情包
 * @property avatarBuffs 头像buff
 * @property emojiPackages 第三方表情包
 * @property blockConfig 屏蔽数据
 */
class App : Application(), HandlerHolder, Handler.Callback {
    lateinit var vegRecordApi: VegRecordApi
    lateinit var database: VegDatabase
    private lateinit var client: OkHttpClient

    @Suppress("PrivatePropertyName")
    private val TAG = App::class.java.simpleName
    override var handler: MyHandler? = null
    lateinit var versionName: String


    override fun onCreate() {
        super.onCreate()
        INSTANCE = this


        // 当前版本号
        this.versionName = packageManager.getPackageInfo(packageName, 0).versionName

        Log.d(TAG, "[APP] APP当前版本: " + this.versionName)
        this.handler = MyHandler(this, this)

        // 初始化数据库
        Log.d(TAG, "[数据库] 初始化")
        val klass = VegDatabase::class.java
        database = Room.databaseBuilder(this, klass, klass.simpleName)
            // 版本硬升级，正式版需移除
//            .fallbackToDestructiveMigration()
            .allowMainThreadQueries()
            .build()

        this.client = OkhttpUtils.obtainOkHttpClient().newBuilder()
            .addInterceptor(LoggingInterceptor())
            .build()

        val retrofit = Retrofit.Builder()
            .baseUrl(host)
            .addConverterFactory(JacksonConverterFactory.create(JacksonUtils.MAPPER))
            .addConverterFactory(QueryParameterConverterFactory())
            .client(client)
            .build()

        // create api
        this.vegRecordApi = retrofit.create(VegRecordApi::class.java)

        PhotoPreview.setGlobalImageLoader { _: Int, source: Any?, imageView: ImageView ->
            if (source is String) Glide.with(imageView.context).load(source.toString())
                .into(imageView)
        }
    }


    companion object {
        const val host = "http://47.102.208.73:16666/"

        const val KB = 1024L
        const val MB = KB * KB

        /**
         * 将APP实例保存到静态变量
         */
        lateinit var INSTANCE: App
    }

    /**
     * 处理消息
     * @param msg Message
     * @return Boolean
     */
    override fun handleMessage(msg: Message) = true
}